package com.jiyun.controller;

import com.jiyun.entity.Employee;
import com.jiyun.service.CityService;
import com.jiyun.service.DeptService;
import com.jiyun.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("controller")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private CityService cityService;

    @RequestMapping("toShow")
    public String toShow(HttpServletRequest request){
        List<Employee> all = employeeService.getAll();
        request.setAttribute("employees",all);
        return "show";
    }

    @RequestMapping("toAdd")
    public String toAdd(HttpServletRequest request){
        return "add";
    }

    @RequestMapping("add")
    public String add(Employee employee){
        employeeService.addEmployee(employee);
        return "redirect:toShow";
    }

    @RequestMapping("toUpdate")
    public String toUpdate(Integer id,HttpServletRequest request){
        Employee employee = employeeService.getEmployee(id);
        request.setAttribute("employee",employee);
        return "update";
    }

    @RequestMapping("update")
    public String update(Employee employee){
        employeeService.updateEmployee(employee);
        return "redirect:toShow";
    }

    @RequestMapping("toDelete")
    public String toDelete(Integer id){
        employeeService.deleteEmployee(id);
        return "redirect:toShow";
    }

}
